<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage core
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: core/application.php 1.0 2009-07-18 13:34 hongbin.hsu $
 */
 
// ------------------------------------------------------------------------

class Application
{
    public static $default_method = 'index';
	protected	$modules		= null;
	private 	$module			= null;
	private		$control		= null;
	private		$method			= null;
	
	//----------------------------------------------------------
	/**
	 * 构造函数
	 *
	 * @param array $config
	 * @return void
	 */
	public function __construct($config = null)
	{
		if (!is_null($config)) {
			if (is_array($config)) {
				$this->modules = $config;
			}else{
				$this->modules['default'] = $config;
			}
		}else{
			$this->modules['default'] = '';
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 设置默认控制器名称
	 *
	 * @param string $control 控制器名称
	 * @return void
	 */
	public function set_controller($control)
	{
		$this->control = strtolower($control);
	}
	
	//----------------------------------------------------------
	/**
	 * 设置默认方法名称
	 *
	 * @param string $method 方法名称
	 * @return void
	 */
	public function set_method($method)
	{
		$this->method = strtolower($method);
		self::$default_method = $this->method;
	}
	
	//----------------------------------------------------------
	/**
	 * 添加模块
	 *
	 * @param string $module	模块名称
	 * @param string $directory	相对于application目录路径
	 * @return void
	 */
	public function add_module($modules = '', $directory = null)
	{
		if (!is_array($modules)) {
			$modules = array($modules => $directory);
		}
		
		foreach ($modules as $module => $directory){
			if (empty($directory)) {
				$this->modules[$module] = $module.DIRECTORY_SEPARATOR;
			}else{
				$this->modules[$module] = $directory;
			}
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 执行应用程序
	 *
	 * @return void
	 */
	public  function execute()
	{
		try {
			Router::dispatch($this->modules, $this->control, $this->method);
			
			$module	 = strtolower(Router::get_module());
	        $control = strtolower(Router::get_controller());
	        $method  = strtolower(Router::get_method());
	        $object  = format_string($control);
	        
	        $control_path = APP_PATH.'controller'.DIRECTORY_SEPARATOR;
	        if (!empty($module))
	        {
	        	$control_path .= $module.DIRECTORY_SEPARATOR;
	        }
	        //$control = str_replace('_', DIRECTORY_SEPARATOR, $control);
	        $controlfile = $control_path.strtolower($control).'.php';
	        
	        if (is_file($controlfile))
	        {
	        	include_once($controlfile);
	        	if (class_exists($object))
	        	{
		        	$app = new $object();
		        	call_user_func_array(array($app, $method), array_slice(Router::get_param(), 0));
		        }else{
		        	throw new Exception("not find {$object}");
		        }
	        }else{
	        	throw new Exception("not find {$controlfile}");
	        }
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
}